/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.broker.proxys;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import sahd.broker.iONetMessageManager.IONetMessageManager;

/**
 *
 * @author Romms
 */
public class ServiceResponseManager {
    private static ServiceResponseManager instance;
    private IONetMessageManager messageManager;

    public static ServiceResponseManager getInstance(Observer observer, int outPort, int entryPort,
                String initialReceiverIpAddress){
        if(instance==null){
            instance=new ServiceResponseManager(observer, outPort, entryPort,initialReceiverIpAddress );
        }
        return instance;
    }

    public ServiceResponseManager getInstance() throws ServiceRequestManagerNotInstanciatedException{
        if(instance!=null){
            return instance;
        }
        throw new ServiceRequestManagerNotInstanciatedException();
    }
    
    private ServiceResponseManager(Observer observer,int outPort, int entryPort,
        String initialReceiverIpAddress){
        try {
            messageManager = new IONetMessageManager(observer, outPort, entryPort, initialReceiverIpAddress);
        } catch (UnknownHostException ex) {
            Logger.getLogger(ServiceResponseManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ServiceResponseManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void waitServiceResponse(){
        messageManager.startReceivingMessages();
    }

    public void addMessageListener(Observer observer){
        this.messageManager.addObserver(observer);
    }

    public void removeMessageListener(Observer observer){
        this.messageManager.removeObserver(observer);
    }

}
